﻿using System;
using System.Collections.Generic;
using UnityEngine;

namespace HurricanePackage
{
    public interface ILoadAble
    {
        void Load(Action _doneAction);

        void Destroy(Action _doAction);

        GameObject GameObj { get; }
    }

    public abstract class AssetObject  : ILoadAble
    {
        private int m_loadCount;//加载次数

        private bool m_isLoaded;//是否加载完成

        private Action m_loadAction;//加载完成的委托
        private Action m_destroyAction;//卸载完成的委托


        public abstract  GameObject GameObj { get; }

        public void Load(Action _doneAction)
        {
            //绑定加载后的回调
            if (_doneAction != null)
                m_loadAction = _doneAction;
            //先判断是否已经加载完成了
            if (m_isLoaded)
            {
                //执行加载回调
                if (m_loadAction != null)
                    m_loadAction();
                m_loadAction = null;

                //引用次数+1
                m_loadCount++;
                return;
            }
            //资源未加载或正在加载的路上~
            else
            {
                if (m_loadCount == 0)
                {
                    //引用次数+1
                    m_loadCount++;
                    _LoadOp();
                }
                //资源正在加载的路上~
               else  if (m_loadCount > 0)
                {
                    //引用次数+1
                    m_loadCount++;
                    return;
                }
                else
                {
#if UNITY_EDITOR
                    Debug.LogError($"{AssetName}在加载时引用次数异常");
#endif
                    m_loadCount = 0;
                }
                
            }

        }
        /// <summary>
        /// 设置加载完成
        /// </summary>
        protected virtual void _SetLoadDone()
        {
            m_isLoaded = true;
            //执行加载回调
            if (m_loadAction != null)
                m_loadAction();
            m_loadAction = null;
        }
        /// <summary>
        /// 重置标识位
        /// </summary>
        private void _Reset()
        {
            m_loadAction = null;
            m_destroyAction = null;
            m_isLoaded = false;
            m_loadCount = 0;
        }


        /// <summary>
        /// 强制卸载
        /// </summary>
        public void ForceDestroy()
        {
            _DestroyOp(m_destroyAction);
            _Reset();
        }

        public void Destroy(Action _doAction)
        {
            if (_doAction != null)
                m_destroyAction = _doAction;
            //尚未加载完成
            if(!m_isLoaded)
                return;
            m_loadCount--;
            if (m_loadCount == 0)
            {
                ForceDestroy();
            }
            //此时说明还有地方用到
            else if(m_loadCount > 0)
            {
                return;
            }
            else
            {
#if UNITY_EDITOR
                Debug.LogError($"{AssetName}在卸载时引用次数异常");
#endif
                m_loadCount = 0;
            }
        }
        /// <summary>
        /// 加载操作
        /// </summary>
        protected abstract void _LoadOp();
        /// <summary>
        /// 卸载操作
        /// </summary>
        protected abstract void _DestroyOp(Action _doneAction);
        /// <summary>
        /// 资源名字
        /// </summary>
        public abstract  string AssetName { get; }
    }
}
